package com.shiguiwu.springmybatis.spring.transaction.demo;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @description: 配置类
 * @author: stone
 * @date: Created by 2022/1/9 11:16
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.transaction.demo
 *
 * 注释掉了，跑整个项目会影响的
 */
//@Configuration
//@ComponentScan
//@EnableTransactionManagement
public class TransactionConfig {




    //@Bean
    //@ConditionalOnMissingBean
    public DataSource dataSource() {
        DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("com.mysql.cj.jdbc.Driver");
        dataSourceBuilder.url("jdbc:mysql://127.0.0.1:3306/lunwen?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai");
        dataSourceBuilder.username("root");
        dataSourceBuilder.password("123456");

        return dataSourceBuilder.build();
    }

    //@Bean
    //@ConditionalOnMissingBean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }


    //@Bean
    //@ConditionalOnMissingBean
    public PlatformTransactionManager manager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }



 }
